<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="设备ID" prop="device_id">
                    <el-select class="w-[280px]" v-model="queryParams.device_id" style="width: 280px;" clearable
                        placeholder="请选择设备ID">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in optionsData.device_all" :key="index" :label="item.name"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" clearable placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input class="w-[280px]" v-model="queryParams.price" clearable placeholder="请输入价格" />
                </el-form-item>
                <el-form-item label="使用时间" prop="use_time">
                    <el-input class="w-[280px]" v-model="queryParams.use_time" clearable placeholder="请输入使用时间" />
                </el-form-item>
                <el-form-item label="是否上架" prop="on_shelf">
                    <el-select class="w-[280px]" v-model="queryParams.on_shelf" style="width: 280px;" clearable
                        placeholder="请选择是否上架">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="(item, index) in dictData.on_shelf" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">

            <el-button v-perms="['boom.boom_device_price/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="设备ID" prop="device_id">
                        <template #default="{ row }">
                            {{ row.device_info == null ? '' : row.device_info.name }}
                        </template>
                    </el-table-column>
                    <el-table-column label="图标" prop="icon">
                        <template #default="{ row }">
                            <el-image style="width:50px;height:50px;" :src="row.icon" />
                        </template>
                    </el-table-column>
                    <el-table-column label="标题" prop="title" show-overflow-tooltip />
                    <el-table-column label="副标题" prop="subhead" show-overflow-tooltip />
                    <el-table-column label="价格" prop="price" show-overflow-tooltip />
                    <el-table-column label="使用时间(分钟)" prop="use_time" show-overflow-tooltip />
                    <el-table-column label="是否上架" prop="on_shelf">
                        <template #default="{ row }">
                            <el-button size="small" type="primary" @click="handleOnShelf(row)">
                                {{ row.on_shelf ? '上架' : '下架' }}
                            </el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="排序" prop="sort" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['boom.boom_device_price/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="boomDevicePriceLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData, useDictOptions } from '@/hooks/useDictOptions'
import { apiBoomDevicePriceLists, apiBoomDevicePriceDelete } from '@/api/device_price'
import { apiBoomDeviceAll, apiBoomShelfPrice } from '@/api/boom_device/index'
import feedback from '@/utils/feedback'




// 查询条件
const queryParams = reactive({
    device_id: '',
    title: '',
    price: '',
    use_time: '',
    on_shelf: '',
    sort: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

const { optionsData } = useDictOptions<{
    device_all: any
}>({
    device_all: {
        api: apiBoomDeviceAll
    }
})

// 获取字典数据
const { dictData } = useDictData('on_shelf')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiBoomDevicePriceLists,
    params: queryParams
})


// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiBoomDevicePriceDelete({ id })
    getLists()
}

const handleOnShelf = async (data: any) => {
    await apiBoomShelfPrice({ id: data.id, on_shelf: data.on_shelf ? 0 : 1 })
    getLists()
}

getLists()
</script>
